package com.chen.androidstudy.utils

import android.content.Context
import com.base.library.utils.ActivityUtils
import kotlin.system.exitProcess

class CrashCollectHandler : Thread.UncaughtExceptionHandler {
    private var mContext: Context? = null

    companion object {
        val instance by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { CrashCollectHandler() }
    }

    fun init(context: Context) {
        this.mContext = context
        Thread.setDefaultUncaughtExceptionHandler(this)
    }

    //当UncaughtException发生时会转入该函数来处理
    override fun uncaughtException(t: Thread, e: Throwable) {
        e.printStackTrace()
        e.postBugly()
        //退出程序
        ActivityUtils.closeAllActivity()
        android.os.Process.killProcess(android.os.Process.myPid())
        exitProcess(0)
    }
}